
// function ajax(url,data){
//     var url_str=serverUrl.base+serverUrl.device+url
//     $.ajax({
//         url: url_str,
//         type: "post",
//         header:{
//             'Access-Control-Allow-Origin':"*"
//         },
//         success:function(data){

//             var req_arr=JSON.parse(data).data
//             console.log(req_arr)
//             $.each(req_arr,function(i,val){
//                 var all_str=``

//                 for(var key in val){
//                     var str=val[key]
//                     ,   new_str=``

//                     new_str+=`
//                         <span>${str}</span>
//                     `
//                     $(li).append(str)
//                     all_str+=new_str
//                 }
//                 var li=`<li class="tab_mes">${all_str}<span>查看详情</span></li>`

//                 $(".mes_list ul").append(li)
//             })
//         }
//     })
// }

//生成列表
function list_ajax(obj) {
    var obj = obj
    $.ajax({
        url: serverUrl.base+serverUrl.device+"report_list",
        type: "post",
        dataType:'json',
        data: obj,
        beforeSend: function () {     //请求成功前触发的局部事件
            $(".lds-css").css("display", "block")
        },
        success: function (data) {

            var req_arr = data.data;

            $(".mes_list ul .tab_mes").remove()
            $(".page_box").html("")
            if (req_arr.length < 1) {
                $(".repairs_box .mes_list ul").css("display", "none")
                $(".repairs_box .mes_list .nolist").css("display", "block")
                return false;
            } else {
                $(".repairs_box .mes_list ul").css("display", "block")
                $(".repairs_box .mes_list .nolist").css("display", "none")
            }
            $.each(req_arr, function (i, val) {
                var all_str = ``
                var _report_txt = report_status_arr //['无',"报修","维修","验收","完成","撤销"]
                
                for (var key in val) {
                    var str = val[key]
                        , new_str = ``
                    if(key == 'report_status'){
                            var _color = report_status_color_arr[val.report_status],_report_status = _report_txt[val.report_status]
                            
                            new_str += `<span style="color:${_color}">${_report_status}</span>`
                    }else{
                            new_str += `<span>${str}</span>`
                    }
                    
                    $(li).append(str)
                    all_str += new_str
                }
                var li = `<li class="tab_mes">${all_str}<span class="detailbtn">查看详情</span></li>`

                $(".mes_list ul").append(li)
                $(".mes_list ul").find("li:last").find('.detailbtn').off('click').on('click',function(elm){
                    $(".repairs_mes_box").css("display", "block")
                    $(".repairs_box").css("display", "none")
                    see_details(serverUrl.base+serverUrl.device+"device_report_details",
                    {
                        reportid: val.report_id,
                        userid: sessionStorage.getItem('user_id') || 1
                    })
                    report_id = val.report_id
                })
            })
            var pagenation = $(data.result_page)
            $(".page_box").append(pagenation)
            $(".lds-css").css("display", "none")
        },
        error:function(res){
            alert('数据加载出现问题')
        },
        complete:function(){
            $(".lds-css").css("display", "none")
        }
    })
}
list_ajax({
    _: Math.random() * 100
})
// 点击查看详情
var report_id,isSeeDetail=false;
var report_status_arr = ["未处理","报修", "维修", "验收", "完成","撤销","超出范围"],
report_status_color_arr = ["#ccc","#FF7B07", "#f00", "#FCF631", "#16E14E","#ccc","#9f00ff"];
function see_details(url, send_data) {
    
    $.ajax({
        url: url,
        type: "post",
        header: {
            'Access-Control-Allow-Origin': "*"
        },
        beforeSend: function () {     //请求成功前触发的局部事件
            $(".lds-css").css("display", "block")
        },
        data: send_data,

        success: function (data) {
            var req_obj = JSON.parse(data).data,
                devinfo = req_obj.devinfo,
                taskinfo = req_obj.taskinfo,
                show_btn = req_obj.status,
                img_arr = devinfo.report_file,
                img_ol,
                report_voice = "",
                audio,
                noimg
            if (devinfo.report_voice) {
                report_voice = devinfo.report_voice
                audio = `<audio src="${report_voice}" controls></audio>`

            } else {
                audio = `<span class="noaudio">无<span>`
            }
            // console.log(img_arr)

            report_id = devinfo.report_id
            $(".repairs_mes_box .mes_list").html()
            
            if (img_arr.length > 0 && img_arr[0] != "") {
                var str = ``
                $.each(img_arr, function (i, val) {
                    if (val != "") {
                        var li = `<li class="img_box">
                                    <img src="${val}" alt="">
                                </li>`
                        str += li
                    }
                })
                img_ol = `<ol>${str}</ol>`
                noimg = ""
            } else {
                noimg = "无"
                img_ol = ``
            }

            var li_list = `<li>设备名称 : <span>${devinfo.device_name}</span></li>
                        <li>设备类型 : <span>${devinfo.device_type_name}</span></li>
                        <li>设备当前状态 : <span style="color:${report_status_color_arr[devinfo.report_status]}">${report_status_arr[devinfo.report_status]}</span></li>
                        <li>设备所在网格 : <span>${devinfo.grid_name}</span></li>
                        <li>问题描述 : <span> ${devinfo.report_remark}</span></li>
                        <li class="img_list">
                            <p>附件 : ${noimg}</p>
                            ${img_ol}
                        </li>
                        <li>语音 :${audio} </li>`
            $(".repairs_mes_box .mes_list").html(li_list)
            //进度详情数据 维修任务的上报状态（报修 维修 验收） 该维修任务状态（超出职能范围）
            taskInfoing(taskinfo,devinfo.report_status,show_btn)
            
            $(".lds-css").css("display", "none")
        },
        complete:function(){
            isSeeDetail = true
        }
    })
}
function taskInfoing(data,devreport,revoke){
    $(".course_box .line_box").remove()
    var taskinfo = data
    var result_arr = ["未处理", "正常", "故障", "超出范围"],
        result_color_arr = ["#ccc", "#16E14E", "#f00", "#ccc"];
        $(taskinfo).each(function (i, val) {
            if (val.status == 1) {
                //报修
                var line_box = `<div class="line_box finish">
                        <span class="num"></span>
                        <ul>
                            <li>故障上报</li>
                            <li>报修人 : <p>${val.username}</p></li>
                            <li>${val.addtime}</li>
                        </ul>
                    </div>
                    `
                $(".cur_info").append(line_box)

            }
            if(val.remark == '' || val.remark == null){
                val.remark = '没有备注'
            }
            if (val.status == 2) {
                //维修
                var line_box = `<div class="line_box finish">
                        <span class="num"></span>
                        <ul>
                            <li style="color:${report_status_color_arr[2]}">设备维修</li>
                            <li>维修人 : <p>${val.username}</p></li>
                            <li>`
                        if(val.taskstatus == '0' ){
                            line_box += `${val.addtime}`
                        }else if(val.taskstatus == '1' && (val.result == '1' || val.result == '2')){
                            line_box += `${val.taskdonetime}`
                        }else if(val.taskstatus == '1' && val.result == '0'){
                            line_box += `${val.tasktime}`
                        }   
                        line_box += `</li>
                        </ul>
                        <div class="rep_tips">
                            <p>备注：${val.remark}</p>
                        </div>
                        <div class="line_box_btn">
                            <button class="transmit">转发任务</button>
                            <button class="revocation">撤销任务</button>
                        </div>
                        <div class="rep_people">
                            <ol>
                                <li>维修状态</li>
                                <li style="color:${result_color_arr[val.result]}">
                                    ${result_arr[val.result]}
                                </li>
                            </ol>
                        </div>
                    </div>`
                $(".cur_info").append(line_box)

            }
            if (val.status == 3) {
                //验收
                var line_box = `<div class="line_box finish">
                        <span class="num"></span>
                        <ul>
                            <li style="color:${report_status_color_arr[3]}">设备验收</li>
                            <li>验收人 : <p>${val.username}</p></li>
                            <li>`
                            if(val.taskstatus == '0' ){
                                line_box += `${val.addtime}`
                            }else if(val.taskstatus == '1' && (val.result == '1' || val.result == '2')){
                                line_box += `${val.taskdonetime}`
                            }else if(val.taskstatus == '1' && val.result == '0'){
                                line_box += `${val.tasktime}`
                            }   
                            line_box += `</li>
                        </ul>
                        <div class="rep_tips">
                            <p>备注：${val.remark}</p>
                        </div>
                        <div class="line_box_btn">
                            <button class="transmit">转发任务</button>
                            <button class="revocation">撤销任务</button>
                        </div>
                        <div class="rep_people">
                            <ol>
                                <li>验收状态</li>
                                <li style="color:${result_color_arr[val.result]}">
                                    ${result_arr[val.result]}
                                </li>
                            </ol>
                        </div>
                    </div>`
                $(".cur_info").append(line_box)
                if (val.result == 1) {
                    var line_box = `<div class="line_box finish">
                                        <span class="num"></span>
                                        <ul>
                                            <li>完成</li>
                                        </ul>
                                    </div>`
                    $(".cur_info").append(line_box)
                }
            }
        })
        if(devreport == '5'){
            var line_box = `<div class="line_box finish">
                                <span class="num"></span>
                                <ul>
                                    <li>已撤销</li>
                                </ul>
                            </div>`
            $(".cur_info").append(line_box)
        }
        
        // var line_box=`<div class="line_box finish">
        //                 <span class="num"></span>
        //                 <ul>
        //                     <li>故障上报</li>
        //                     <li>报修人 : <p>${taskinfo[0].username}</p></li>
        //                     <li>${taskinfo[0].addtime}</li>
        //                 </ul>
        //             </div>
        //             <div class="line_box">
        //                 <span class="num"></span>
        //             </div>
        //             `
        // console.log(devinfo.report_status, show_btn)

        //revoke 维修任务状态 超出职能范围

        if (revoke == 3 && devreport != 5 && powers.revoke_device_report && powers.repeat_device_report) {
            $(".line_box").last().find(".line_box_btn").css("display", "block")
        }

        $(".finish").last().css("border-top-color", "transparent")
        if ($(".line_box").length == 1) {
            var lis = `<div class="line_box finish"><span class="num"></span></div>`
            $(".cur_info").append(lis)
            $(".line_box").first().css("border-top-color", "#11a0f8")
            $(".finish").last().css("border-top-color", "transparent")
        }
        var len = $(".line_box").length
        $(".cur_info").width(320 * len + "px")
}
$(".cur_tit").on("click", function () {
    $(".repairs_mes_box").css("display", "none")
    $(".repairs_box").css("display", "block")
    isSeeDetail = false
})
// select插件  获取设备类型，状态，网格
var dev_type_arr = [{ id: "", text: "请选择" }],
    dev_status_arr = [{ id: "", text: "请选择" }, { id: "1", text: "报修" }, { id: "2", text: "维修" }, { id: "3", text: "验收" }, { id: "4", text: "完成" },],
    dev_gr_arr = [{ id: "", text: "请选择" }]

$.ajax({
    url: serverUrl.base+serverUrl.device+"type_list",
    data: {
        sel: 1
    },
    success: function (data) {
        var res_arr = JSON.parse(data).data

        for (var i = 0; i < res_arr.length; i++) {
            dev_type_arr.push(res_arr[i])
        }
        $(".devicetype").select2({
            data: dev_type_arr,
            placeholder: '请选择'
        })
    }
})
$.ajax({
    url: serverUrl.base+serverUrl.grid+"grid_choice_list",
    data: {
        sel: 1
    },
    success: function (data) {
        var res_arr = JSON.parse(data).data
        for (var i = 0; i < res_arr.length; i++) {
            dev_gr_arr.push(res_arr[i])
        }
        $(".grid").select2({
            data: dev_gr_arr,
            placeholder: '请选择'

        })

    }
})
$(".status").select2({
    data: dev_status_arr,
    placeholder: '请选择',

})
// 时间插件
$('.datetimepicker').datetimepicker({
    lang: 'ch',
    format:"Y-m-d H:i"
});

//设备报修点击搜索按钮
$(".repairs_box .search_btn").on("click", function () {

    var devicename = $(".devicename").val(),
        devicetype = $('.devicetype').select2('data')[0].id,
        status = $('.status').select2('data')[0].id,
        grid = $('.grid').select2('data')[0].id,
        starttime = $(".starttime").val(),
        endtime = $(".endtime").val()
    if (devicetype == "请选择") {
        devicetype = ""
    }
    if (status == "请选择") {
        status = ""
    }
    if (grid == "请选择") {
        grid = ""
    }
    if (starttime == "请选择开始时间") {
        starttime = ""
    }
    if (endtime == "请选择结束时间") {
        endtime = ""
    }
    var sen_obj = {
        devicename: devicename,
        devicetype: devicetype,
        status: status,
        grid: grid,
        starttime: starttime,
        endtime: endtime

    }
    list_ajax(sen_obj)


})

//设备报修点击重置按钮
$(".repairs_box .reset_btn").on("click", function () {
    $(".devicename").val("")
    // $(".devicetype option").eq(0).prop("selected","true")
    $(".devicetype").select2("val", "");//  回复默认
    $(".status").select2("val", "");
    $(".grid ").select2("val", "");
    $(".starttime").val("");
    $(".endtime").val("");
})

// 分页

// $(".pagination a").on("click", function () {
//     $(this).addClass("disabled").siblings("a").removeClass("disabled")
// })
function ajax_data(num) {
    list_ajax({
        _: Math.random() * 100,
        page: num
    })
}
// 跳转
function pageIndex(go_btn) {
    var chan_ipt = $(go_btn).prev(),
        page_num = Number(chan_ipt.val())
    if (!isNaN(page_num)) {
        ajax_data(page_num)
    }
}
// $(".btn_page").on("click", function () {
//     var page_num = $(".changePage").val()
//     pageIndex(page_num)

// })


// 任务撤销
$(".course_box").on("click", function (e) {
    var tar_text = $(e.target).text()
    if (tar_text == "撤销任务") {
        $(".lds-css").css("display", "block")
        $.ajax({
            url: serverUrl.base+serverUrl.device+"get_repairedevice_result",
            type: "post",
            data: {
                reoprtid: report_id,
                type: 2,
                userid:sessionStorage.getItem('user_id') || 1,
            },
            header: {
                'Access-Control-Allow-Origin': "*"
            },
            success: function (data) {
                var result = JSON.parse(data),
                    code = result.code,
                    info = result.info
                alert(info)
            },
            complete:function(){
                $(".lds-css").css("display", "none")
            }

        })
    }
    if (tar_text == "转发任务") {
        $(".tran_box").css("display", "block")
        $(".mark_box").css("display", "block")
    }
})
$("#showipt").click(function () {
    showsDeptWin('showipt', 'hideipt', '4', false);
})
$(".tran_box .send_job").on("click", function () {
    var uid = $("#hideipt").val(),
        jobendtime = $(".job_endtime").val()
    if (uid == "" || jobendtime == "") {
        $(".attention").css("opacity", "1")
    } else {
        $(".lds-css").css("display", "block")
        $.ajax({
            url: serverUrl.base+serverUrl.device+"get_repairedevice_result",
            type: "post",
            data: {
                reoprtid: report_id,
                type: 1,
                userid: sessionStorage.getItem('user_id') || 1,
                uid: uid,
                endtime: jobendtime
            },
            header: {
                'Access-Control-Allow-Origin': "*"
            },
            success: function (data) {
                var result = JSON.parse(data),
                    info = result.info,
                    code = result.code
                alert(info)
                $("#hideipt").val("")
                $("#showipt").val("")
                $(".job_endtime").val("")
            },
            complete:function(){
                $(".lds-css").css("display", "none")
            }

        })
        $(".mark_box").css("display", "none");
        $(".tran_box").css("display", "none");
        $(".attention").css("opacity", "0");

    }


})
$('.course_box_title button.refreshbtn').on('click',function(){
    $.post(serverUrl.base+serverUrl.device+"device_report_details",
                    {
                        reportid: report_id,
                        userid: sessionStorage.getItem('user_id') || 1
                    },function(res){
                        if(res.code == '0'){
                            taskInfoing(res.data.taskinfo,res.data.devinfo.report_status,res.data.status)
                        }
                    },'json')
})

var set_int = setInterval(function(){
    if(report_id != undefined && isSeeDetail){
        $('.course_box_title button.refreshbtn').trigger('click')
    }
},30000)
$(".tran_box .cancle").on("click", function () {
    $(".tran_box").css("display", "none");
    $(".mark_box").css("display", "none");
    $(".attention").css("opacity", "0");
    $("#hideipt").val("")
    $("#showipt").val("")
    $(".job_endtime").val("")
})





